package com.atguigu.tx;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

/**
 * @Author:zwy
 * @Despriction:
 * @Date:Create in 17:15 2018/7/10
 * @Modify By:
 *
 * 1 导入相关依赖Spring-jdbc模块
 * 配置数据源、JdbcTemplate(Spring提供的简化数据库操作的工具)操作数据
 *
 */
@Configuration
@ComponentScan("com.atguigu.tx")
public class Txconfig {
    //数据源
    @Bean
    public DataSource dataSource() throws Exception {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
        dataSource.setUser("root");
        dataSource.setPassword("root");
        dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/abc?useSSL=false");
        return dataSource;

    }
    @Bean
    public JdbcTemplate jdbcTemplate() throws Exception {
        //Spring对Configuration类会特殊处理
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource());
        return jdbcTemplate;
    }
}
